package by.imotaeva.printing_services.services.impl;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.imotaeva.printing_services.dataaccess.UserAccountDao;
import by.imotaeva.printing_services.datamodel.UserAccount;
import by.imotaeva.printing_services.services.UserAccountService;

@Service
public class UserAccountServiceImpl implements UserAccountService {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(UserAccountServiceImpl.class);

	@Inject
	private UserAccountDao dao;

	@PostConstruct
	private void init() {
		// this method will be called by Spring after bean instantiation. Can be
		// used for any initialization process.
		LOGGER.info("Instance of UserAccountService is created. Class is: {}",
				getClass().getName());
	}

	@Override
	public UserAccount getUser(Long id) {
		UserAccount entity = dao.getById(id);
		return entity;
	}

	@Override
	public void saveNewUser(UserAccount userAccount) {
		if (userAccount.getId() == null) {
			LOGGER.debug("Save new: {}", userAccount);
			dao.insert(userAccount);
		}
		// сделать проверку на уникальность role

	}

	@Override
	public void updateUser(UserAccount userAccount) {
		if (userAccount.getId() != null) {
			LOGGER.debug("Update: {}", userAccount);
			dao.update(userAccount);
		}

	}

	@Override
	public void removeUser(UserAccount userAccount) {
		LOGGER.debug("Remove: {}", userAccount);
		dao.delete(userAccount.getId());
	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Remove all users: {}");
		dao.deleteAll();
	}

}
